package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.studyrecord;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

/**
 * 学习记录-列表
 * 
 */
@CrossOrigin
@RestController
public class TifenbaoStudyRecoredListController extends YxyApiBaseController {

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/studyrecord/recored/list", method = RequestMethod.POST)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws ParseException {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);
        Page<YxyHuixuePlan> yxyHuixuePlanLst = yxyHuixuePlanRepository
                .findByStudentIdAndStatusAndCurrentScoreGreaterThanOrderByLastModifiedDateDesc(reqModel.studentId, 0,
                        -1, pageable);
        List<RespModel> models = new ArrayList<RespModel>();
        RespModel model = null;
        for (YxyHuixuePlan yxyHuixuePlan : yxyHuixuePlanLst) {
            model = new RespModel();
            model.planId = yxyHuixuePlan.getId();
            model.subjectName = yxyHuixuePlan.getSubjectName();
            model.versionName = yxyHuixuePlan.getVersionName();
            model.cebieName = yxyHuixuePlan.getSemesterName();
            model.bookContentName = yxyHuixuePlan.getBookContentName();
            model.lastScore = yxyHuixuePlan.getLastScore();
            model.expectScore = yxyHuixuePlan.getExpectScore();
            double expect = model.lastScore + model.expectScore;
            model.status = yxyHuixuePlan.getCurrentScore() >= expect ? 0 : 1;
            models.add(model);
        }
        PageImpl<RespModel> pageImpl = new PageImpl<RespModel>(models, pageable, yxyHuixuePlanLst.getTotalElements());
        Page<RespModel> pageLst = pageImpl;
        return pageLst;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 学习计划id
        public String planId;
        // 学科名称
        public String subjectName;
        // 版本名称
        public String versionName;
        // 册别名称
        public String cebieName;
        // 章节名称
        public String bookContentName;
        // 初始分数
        public Double lastScore;
        // 预期提高分数: 5, 10
        public Integer expectScore;
        // 学习计划的状态：0--计划完成 , 1--计划失败
        public Integer status;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 学生id
        public String studentId;
    }
}
